package com.cfgj.web;

import com.cfgj.entity.CrowdFunding;
import com.cfgj.mapper.CrowdFundingMapper;
import com.cfgj.service.CrowdFundingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/crowdfunding")
public class CrowdFundingController {

    @Autowired
    private CrowdFundingService crowdFundingService;

    @Autowired
    private CrowdFundingMapper crowdFundingMapper;

    @RequestMapping("/findList")
    public String shenheList(String keyword ,Model model, Integer page, Integer size){
        if (page==null||page==0) {
            page = 1;
        }
        if(keyword == null){
            keyword = "";
        }
        List<CrowdFunding> lists = crowdFundingService.findCrowfundingbyname("'%"+keyword+"%'",page,size);
        Integer counts = crowdFundingService.findcount("1");
        model.addAttribute("count",counts);
        model.addAttribute("list",lists);
        model.addAttribute("curr",page);
        return "admin/zhongchou_list";
    }

    @RequestMapping("/firstList")
    public String shenheList(Model model, Integer page, Integer size){
        if (page==null||page==0) {
            page = 1;
        }
        List<CrowdFunding> lists = crowdFundingService.findCrowfundingbyStatus("0",page,size);
        Integer counts = crowdFundingService.findcount("0");
        model.addAttribute("count",counts);
        model.addAttribute("list",lists);
        model.addAttribute("curr",page);
        return "admin/zhongchou_list";
    }

    @RequestMapping("/pass")
    @ResponseBody
    public String pass(Integer crowdId){
         System.out.println(crowdId);
         try {
             crowdFundingService.passCrowdFund(crowdId);
         }
         catch (Exception e){
             return "0";
         }

        return "1";
    }
    @RequestMapping("info")
    public String crodInfo(Integer crowdId,Model model){
        CrowdFunding crowdFunding=crowdFundingMapper.findCrowFuningById(crowdId);
        model.addAttribute("crowd",crowdFunding);
        return "admin/zcinfo";
    }

    //众筹展示
    @RequestMapping("/crowdfunding_list")
    public String crowdfunding_list(Model model, Integer page, Integer size){
        if (page==null||page==0) {
            page = 1;
        }
        List<CrowdFunding> lists = crowdFundingService.findCrowfundingbyStatus("1",page,size);
        Integer counts = crowdFundingService.findcount("1");
        model.addAttribute("count",counts);
        model.addAttribute("list",lists);
        model.addAttribute("curr",page);
        return "admin/crowdfunding-list";
    }



}
